package com.shuai.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shuai.model.bo.AllotBo;
import com.shuai.model.po.SysPermission;
import com.shuai.model.query.SysPermissionQuery;
import com.shuai.model.vo.SysPermissionVo;
import com.shuai.model.vo.SysRouterVO;
import com.shuai.service.SysPermissionService;
import com.shuai.utils.returnVo.ReturnModel;
import com.shuai.utils.tree.SysTreeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限信息 前端控制器
 *
 * @author shuai
 * @since 2022
 */
@Slf4j
@RestController
@RequestMapping("/sysPermission")
public class SysPermissionController {

    @Resource
    private SysPermissionService sysPermissionService;

    /**
     * 添加权限信息
     * @param sysPermission
     * @return
     */
    @PostMapping("/addSysPermission")
    public ReturnModel addSysPermission(@RequestBody SysPermission sysPermission){
        sysPermissionService.addSysPermission(sysPermission);
        return ReturnModel.successInstance();
    }

    /**
     * 删除权限信息
     * @param query
     * @return
     */
    @RequestMapping ("/deleteSysPermission")
    public ReturnModel deleteSysPermission(@RequestBody SysPermissionQuery query){
        sysPermissionService.deleteSysPermission(query);
        return ReturnModel.successInstance();
    }

    /**
     * 更新权限信息
     * @param sysPermission
     * @return
     */
    @PostMapping("/editSysPermission")
    public ReturnModel editSysPermission(@RequestBody SysPermission sysPermission){
        sysPermissionService.editSysPermission(sysPermission);
        return ReturnModel.successInstance();
    }

    /**
     * 查询权限信息
     * @param query
     * @return
     */
    @PostMapping("/selectList")
    public ReturnModel<List<SysPermissionVo>> selectList(@RequestBody SysPermissionQuery query){
        List<SysPermissionVo> list = sysPermissionService.selectList(query);
        return ReturnModel.successInstance(list);
    }

    /**
     * 分页查询权限信息
     * @param query
     * @return
     */
    @PostMapping("/selectPage")
    public ReturnModel<IPage<SysPermissionVo>> selectPage(@RequestBody SysPermissionQuery query){
        IPage<SysPermissionVo> list = sysPermissionService.selectPage(query);
        return ReturnModel.successInstance(list);
    }

    /**
     * 查询详细
     * @param id
     * @return
     */
    @GetMapping ("/selectById")
    public ReturnModel<SysPermissionVo> selectById(@RequestParam() Long id){
        SysPermissionVo permissionVo = sysPermissionService.selectById(id);
        return ReturnModel.successInstance(permissionVo);
    }

    /**
     * 获取用户菜单
     * @param roles
     * @return
     */
    @PostMapping("/getUserMenuInfo")
    public ReturnModel getUserMenuInfo(@RequestBody List<Long> roles){
        List<SysRouterVO> menuList = sysPermissionService.getUserMenuInfo(roles);
        return ReturnModel.successInstance(menuList);
    }

    /**
     * 查询权限树
     * @param query
     * @return
     */
    @PostMapping("/selectTreeList")
    public ReturnModel<List<SysPermissionVo>> selectTreeList(@RequestBody SysPermissionQuery query){
        List<SysPermissionVo> list = sysPermissionService.selectTreeList(query);
        return ReturnModel.successInstance(list);
    }

    /**
     * 查询权限树
     * @param query
     * @return
     */
    @PostMapping("/selectPermissionTree")
    public ReturnModel<List<SysTreeVo>> selectPermissionTree(@RequestBody SysPermissionQuery query){
        List<SysTreeVo> list = sysPermissionService.selectPermissionTree(query);
        return ReturnModel.successInstance(list);
    }

    /**
     * 查询角色权限信息和权限列表
     * @param roleId
     * @return
     */
    @GetMapping("/selectRolePermissionList")
    public ReturnModel<AllotBo> selectRolePermissionList(@RequestParam() Long roleId){
        AllotBo bo = sysPermissionService.selectRolePermissionList(roleId);
        return ReturnModel.successInstance(bo);
    }

}

